layui.use(['table', 'jquery'], function () {
    const table = layui.table,
        $ = layui.jquery;

    //默认加载时执行
    findAll();

    function findAll() {

        let code = $('#code').val();
        let name = $('#name').val();
        let params = {
            code: code,
            name: name
        };

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'code', width: '20%', title: '角色编号'}
            , {field: 'name', width: '20%', title: '角色名'}
            , {field: 'createTime', width: '20%', title: '创建时间', sort: true}
            , {field: 'updateTime', width: '20%', title: '更新时间', sort: true}
            , {fixed: 'right', width: '15%', align: 'center', toolbar: '#operating'}
        ]];

        //发送请求
        table.render({
            elem: '#dataList'
            , url: AUTH_SERVER_URL + "/role/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 15, 30]
            , limit: 10
            , title: '角色数据表'
            , cols: cols

        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data;
        switch (obj.event) {
            case 'add':
                addRole();
                break;
        }
    });

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data
            , layEvent = obj.event;
        if (layEvent === 'del') {
            layer.confirm('此操作不可逆，确定删除？', function (index) {
                layer.close(index);
                deleteRole(data.id);
            });
        } else if (layEvent === 'edit') {
            updateRole(data.id);
        }
    });


    /**
     * 添加
     */
    function addRole() {
        layer.open({
            title: '新增角色',
            area: ["400px", "240px"],
            shadeClose: true,
            anim: 2,
            type: 2,
            content: 'roleAdd.html'
        });
    }

    /**
     * 删除
     * @param id
     */
    function deleteRole(id) {

        let result = commonAjax(AUTH_SERVER_URL+'/role/delete', {id: id}, 'get');
        if (result.code === 200) {
            layerMsgDeleteSuccess();
            //刷新
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }

    /**
     * 修改
     * @param id
     */
    function updateRole(id) {
        sessionStorage.setItem("roleId", id);
        layer.open({
            title: '角色详情',
            area: ["400px", "240px"],
            shadeClose: true,
            anim: 2,
            type: 2,
            content: 'roleDetails.html'
        });
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll();
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#code').val('');
        $('#name').val('');
    });
});